﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Alfred.Ressource;

namespace Alfred
{
    class XML
    {
        public static void AddToXml(string id, List<string> items)
        {
            string path = Paths.path_grammar;
            XmlDocument xml = new XmlDocument();

            xml.Load(path);

            XmlNodeList rules = xml.GetElementsByTagName("rule");
            XmlNode oneOf = null;

            foreach (XmlNode node in rules)
            {
                if (node.Attributes["id"].Value ==id)
                {
                    oneOf = node.ChildNodes[0].ChildNodes[0].ChildNodes[0];
                }
            }

            oneOf.RemoveAll();
            foreach (string s in items)
            {
                XmlElement newFilm = xml.CreateElement("item", "http://www.w3.org/2001/06/grammar");
                newFilm.InnerText = s;
                oneOf.AppendChild(newFilm);
            }

            xml.Save(path);
        }
    }
}
